﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmChiTienHang : Form
    {
        private MuaHangManagement muahangMgt = new MuaHangManagement();
        private PhieuNhapKhoMgt phieunhapkhoMgt = new PhieuNhapKhoMgt();
        private PhieuChiTienManagement phieuchitienMgt = new PhieuChiTienManagement();
        private CTPhieuNhapKhoMgt ctphieunhapkhoMgt = new CTPhieuNhapKhoMgt();
        private CTMuaHangManagement ctmuahangMgt = new CTMuaHangManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmChiTienHang()
        {
            InitializeComponent();
        }

        private void FrmChiTienHang_Load(object sender, EventArgs e)
        {
            dgvPhieuMuaHang.AutoGenerateColumns = false;
            dgvPhieuNhapKho.AutoGenerateColumns = false;

            radioPhieuMuaHang.Checked = true;
            dgvPhieuNhapKho.Visible = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        } 

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            if (radioPhieuMuaHang.Checked)
            {
                List<MuaHang> lstMuaHang = (from mh in GetAllMuaHang()
                                            where (mh.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                mh.NguoiLap.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                mh.NgayThang.Date >= dtTuNgay.Date && mh.NgayThang.Date <= dtDenNgay.Date
                                            select mh).ToList();

                dgvPhieuMuaHang.DataSource = lstMuaHang;
            }
            else if (radioPhieuNhapKho.Checked)
            {
                List<PhieuNhapKho> lstPhieuNhapKho = (from pnk in GetAllPhieuNhapKho()
                                                      where (pnk.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                            pnk.NguoiLap.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                            pnk.NgayThang.Date >= dtTuNgay.Date && pnk.NgayThang.Date <= dtDenNgay.Date
                                                      select pnk).ToList();

                dgvPhieuNhapKho.DataSource = lstPhieuNhapKho;
            }            
        }

        private void dgvPhieuNhapKho_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
           
            PhieuNhapKho phieunhapkho = (PhieuNhapKho)dgvPhieuNhapKho.CurrentRow.DataBoundItem;
            List<PhieuChiTien> lstPhieuChiTien = (from pct in GetAllPhieuChiTien()
                                                  where pct.IdNhapHang == phieunhapkho.Id
                                                  && pct.IsDelete==0
                                                  select pct).ToList();
            List<CTPhieuNhapKho> lstCTPhieuNhapKho = (from ctp in GetAllCTPhieuNhapKho()
                                                      where ctp.IdPhieuNhapKho == phieunhapkho.Id &&
                                                      ctp.IsDelete==0
                                                      select ctp).ToList();

            long sotienPhieuChiTien = 0;
            long sotienPhieuNhapKho = 0;

            for (int i = 0; i < lstPhieuChiTien.Count; i++)
            {
                sotienPhieuChiTien += lstPhieuChiTien[i].SoTien;
            }

            for (int i = 0; i < lstCTPhieuNhapKho.Count; i++)
            {
                sotienPhieuNhapKho += lstCTPhieuNhapKho[i].ThanhTien;
            }

            //chua tra tien gi ca
            if (lstPhieuChiTien.Count > 0)
            {
                //kiem tra da tra du tien
                if (sotienPhieuChiTien == sotienPhieuNhapKho)
                {
                    MessageBox.Show("Phiếu đã đủ.", "Thông báo");
                }
                //chua tra du tien
                else if(sotienPhieuNhapKho > sotienPhieuChiTien)
                {
                    FrmPhieuChiTien frmPhieuChiTien = new FrmPhieuChiTien(phieunhapkho, sotienPhieuNhapKho - sotienPhieuChiTien,sotienPhieuChiTien,this.nhanvien,false);
                    frmPhieuChiTien.Show();
                }
            }
            else
            {
                FrmPhieuChiTien frmPhieuChiTien = new FrmPhieuChiTien(phieunhapkho, sotienPhieuNhapKho,0, this.nhanvien, false);
                frmPhieuChiTien.Show();
            }
        }

        private void dgvPhieuMuaHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            MuaHang muahang = (MuaHang)dgvPhieuMuaHang.CurrentRow.DataBoundItem;
            List<PhieuChiTien> lstPhieuChiTien = (from pct in GetAllPhieuChiTien()
                                                  where pct.IdMuaHang == muahang.Id &&
                                                  pct.IsDelete==0
                                                  select pct).ToList();

            List<CTMuaHang> lstCTMuaHang = (from ctp in GetAllCTMuaHang()
                                            where ctp.IdMuaHang == muahang.Id &&
                                            ctp.IsDelete==0
                                            select ctp).ToList();

            long sotienPhieuChiTien = 0;
            long sotienPhieuMuaHang = 0;

            for (int i = 0; i < lstPhieuChiTien.Count; i++)
            {
                sotienPhieuChiTien += lstPhieuChiTien[i].SoTien;
            }

            for (int i = 0; i < lstCTMuaHang.Count; i++)
            {
                sotienPhieuMuaHang += (lstCTMuaHang[i].DonGia * lstCTMuaHang[i].SoLuong);
            }

            //chua tra tien gi ca
            if (lstPhieuChiTien.Count > 0)
            {
                //kiem tra da tra du tien
                if (sotienPhieuChiTien == sotienPhieuMuaHang)
                {
                    MessageBox.Show("Phiếu đã đủ.", "Thông báo");
                }
                //chua tra du tien
                else if (sotienPhieuMuaHang > sotienPhieuChiTien)
                {
                    FrmPhieuChiTien frmPhieuChiTien = new FrmPhieuChiTien(muahang, sotienPhieuMuaHang - sotienPhieuChiTien,sotienPhieuChiTien, this.nhanvien, false);
                    frmPhieuChiTien.Show();
                }
            }
            else
            {
                FrmPhieuChiTien frmPhieuChiTien = new FrmPhieuChiTien(muahang, sotienPhieuMuaHang, 0,this.nhanvien, false);
                frmPhieuChiTien.Show();
            }
        }

        private void radioPhieuMuaHang_CheckedChanged(object sender, EventArgs e)
        {
            dgvPhieuNhapKho.Visible = false;
            dgvPhieuMuaHang.Visible = true;

            //dgvPhieuMuaHang.DataSource = GetAllMuaHang();
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;
            List<MuaHang> lstMuaHang = (from mh in GetAllMuaHang()
                                        where (mh.MaSo.Contains(txtTimKiem.Text) ||
                                            mh.NguoiLap.Contains(txtTimKiem.Text)) &&
                                            mh.NgayThang.Date >= dtTuNgay.Date && mh.NgayThang.Date <= dtDenNgay.Date &&
                                            mh.IsDelete==0

                                        select mh).ToList();
            dgvPhieuMuaHang.DataSource = lstMuaHang;
        }

        private void radioPhieuNhapKho_CheckedChanged(object sender, EventArgs e)
        {
            dgvPhieuNhapKho.Visible = true;
            dgvPhieuMuaHang.Visible = false;

            //dgvPhieuNhapKho.DataSource = GetAllPhieuNhapKho();
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            List<PhieuNhapKho> lstPhieuNhapKho = (from pnk in GetAllPhieuNhapKho()
                                                  where (pnk.MaSo.Contains(txtTimKiem.Text) ||
                                                        pnk.NguoiLap.Contains(txtTimKiem.Text)) &&
                                                        pnk.NgayThang.Date >= dtTuNgay.Date && pnk.NgayThang.Date <= dtDenNgay.Date &&
                                                        pnk.IsDelete==0
                                                  select pnk).ToList();

            dgvPhieuNhapKho.DataSource = lstPhieuNhapKho;
        }

        public List<MuaHang> GetAllMuaHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = muahangMgt.GetAllMuaHang();
            List<MuaHang> listMuaHang = (from mh in(List<MuaHang>)resultObj.Result
                                             where mh.IsDelete==0 
                                         select mh).ToList();

            return listMuaHang;
        }

        public List<PhieuNhapKho> GetAllPhieuNhapKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieunhapkhoMgt.GetAllPhieuNhapKho();
            List<PhieuNhapKho> listPhieuNhapKho = (from pnk in(List<PhieuNhapKho>)resultObj.Result
                                                       where pnk.IsDelete==0
                                                       select pnk).ToList();

            return listPhieuNhapKho;
        }

        public List<PhieuChiTien> GetAllPhieuChiTien()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuchitienMgt.GetAllPhieuChiTien();
            List<PhieuChiTien> listPhieuChiTien = (from pct in (List<PhieuChiTien>)resultObj.Result
                                                       where pct.IsDelete==0
                                                       select pct).ToList();

            return listPhieuChiTien;
        }

        public List<CTPhieuNhapKho> GetAllCTPhieuNhapKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = ctphieunhapkhoMgt.GetAllCTPhieuNhapKho();
            List<CTPhieuNhapKho> listCTPhieuNhapKho =(from ctp in(List<CTPhieuNhapKho>)resultObj.Result
                                                          where ctp.IsDelete==0
                                                          select ctp).ToList();

            return listCTPhieuNhapKho;
        }

        public List<CTMuaHang> GetAllCTMuaHang()
        {
            ResultObj resultObj = new ResultObj();
            
            resultObj = ctmuahangMgt.GetAllCTMuaHang();
            List<CTMuaHang> listCTMuaHang = (from ct in(List<CTMuaHang>)resultObj.Result
                                                 where ct.IsDelete==0
                                                 select ct).ToList();

            return listCTMuaHang;
        }
    }
}
